package com.gopay.common.cps.dao.ic.impl;

import com.gopay.common.PageInfo;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.ic.IcSuccessConfigDAO;
import com.gopay.common.domain.ic.IcSuccessConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * @ClassName: IcSuccessOrderDtlDAOImpl 
 * @Description: 成功订单金额校验
 * @author liuyu 
 * @date 2016年12月22日 下午4:26:17  
 */
@Repository("icSuccessConfigDAOImpl")
public class IcSuccessConfigDAOImpl extends BaseDAOImpl<IcSuccessConfig, Serializable> implements IcSuccessConfigDAO{
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(IcSuccessConfigDAOImpl.class);

    @Override
    public List<IcSuccessConfig> findByCondition(Map<String, String> params, PageInfo pageInfo) {
        StringBuilder hql = new StringBuilder("from IcSuccessConfig i where 1=1");
        Map<String, Object> paramNew = new HashMap<>(3);
        if(StringUtils.isNotBlank(params.get("accountingPoint"))){

            hql.append(" and accountingPoint = :accountingPoint" );
            paramNew.put("accountingPoint",params.get("accountingPoint"));
        }
        if(StringUtils.isNotBlank(params.get("intTxnCd"))){
            hql.append(" and intTxnCd = :intTxnCd" );
            paramNew.put("intTxnCd",params.get("intTxnCd"));
        }
        if(StringUtils.isNotBlank(params.get("groups"))){
            hql.append(" and groups = :groups" );
            paramNew.put("groups",params.get("groups"));
        }
        hql.append(" order by intTxnCd");
        List<IcSuccessConfig> result = findByHql(hql.toString(), paramNew, pageInfo.getPageNum(),pageInfo.getPageSize());

        StringBuilder sbCount = new StringBuilder("select count(*) ");
        sbCount.append(hql);
        pageInfo.setTotalCount(findCountByHql(sbCount.toString(), paramNew));
        return result;
    }

    @Override
    public void updateById(IcSuccessConfig icSuccessConfig) {
        Map<String,Object> map = new HashMap();
        StringBuffer  sql = new StringBuffer();
        sql.append("update IcSuccessConfig w set w.intTxnCd=:intTxnCd , w.intTxnName=:intTxnName," +
                "w.accountingPoint=:accountingPoint,w.payChannel=:payChannel,w.groups=:groups,w.memo=:memo,w.gmtModify=sysdate" +
                " where w.id=:id" );
        map.put("intTxnCd",icSuccessConfig.getIntTxnCd());
        map.put("intTxnName",icSuccessConfig.getIntTxnName());
        map.put("accountingPoint",icSuccessConfig.getAccountingPoint());
        map.put("payChannel",icSuccessConfig.getPayChannel());
        map.put("groups",icSuccessConfig.getGroups());
        map.put("memo",icSuccessConfig.getMemo());
        map.put("id",icSuccessConfig.getId());
        updateByHql(sql.toString(),map);
    }

    @Override
    public int deleteById(Long id){
        StringBuffer  sql = new StringBuffer();
        sql.append("delete from IcSuccessConfig where id=");
        sql.append(id);
        int result = deleteByHql(sql.toString());
        return result;
    }
}
